package com.huicoo.forestmanager.ui.patrol.PatrolRecordChildFragment.presenter;


import android.text.TextUtils;

import com.huicoo.forestmanager.network.bean.patrol.RecordlistBean;
import com.huicoo.forestmanager.ui.patrol.PatrolRecordChildFragment.contract.PatrolRecordChildFragmentContract;
import com.huicoo.forestmanager.ui.patrol.PatrolRecordChildFragment.model.PatrolRecordChildFragmentModel;
import com.huicoo.forestmanager.util.JsonUtils;
import com.huicoo.forestmanager.util.LogUtils;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class PatrolRecordChildFragmentPresenter implements PatrolRecordChildFragmentContract.Presenter {
    private PatrolRecordChildFragmentContract.View view;
    private PatrolRecordChildFragmentContract.Model model;

    public PatrolRecordChildFragmentPresenter(PatrolRecordChildFragmentContract.View view) {
        this.view = view;
        this.model = new PatrolRecordChildFragmentModel();
    }


    @Override
    public void destroy() {
        view = null;
    }


    //获取巡查记录
    @Override
    public void getrecordlist(String time) {
        Call<RecordlistBean> call = model.getrecordlist(time);
        call.enqueue(new Callback<RecordlistBean>() {
            @Override
            public void onResponse(Call<RecordlistBean> call, Response<RecordlistBean> response) {
                try {
                    RecordlistBean body = response.body();
                    if (body == null) {
                        //数据请求失败
                        if (view != null) {
                            view.getrecordlistFail("数据为空");
                        }
                        return;
                    }
                    //数据请求成功
                    LogUtils.e("gogogo 获取巡查记录月份 result = " + JsonUtils.toJson(response.body()));

                    if (response.body() != null && TextUtils.equals(response.body().getCode(), "200")) {
                        if (view != null) {
                            view.getrecordlistSuccess(body);
                        }
                    } else {
                        if (view != null) {
                            view.getrecordlistFail(TextUtils.isEmpty(response.body().getErrorMsg()) ? "数据为空" : response.body().getErrorMsg());
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<RecordlistBean> call, Throwable t) {
                if (view != null) {
                    view.getrecordlistFail(t.getMessage() + "");
                }
                LogUtils.v("gogogo fail fail fail ");
                t.printStackTrace();
            }
        });
    }


}
